%Figure 4
%Perform comparative statics in kappa, phi, Lambda, and gamma


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% KAPPA %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%


clear all; close all;
para;

%Note we choose a fine gride and low tolerance to obtain numerically
%accurate solutions. This increases the computation time
%Inaccuracy only arises for very high values of kappa
qGrid=600;
options = odeset('AbsTol',1e-10,'RelTol',1e-10);

%For lower accuracy and computation time set, e.g., qGrid=200, options = odeset('AbsTol',1e-6,'RelTol',1e-6);

iComp=50;
%To speed up computation, choose lower iComp

kappaSpace=linspace(6.5,40,iComp);

for comp=1:iComp
    
    kappa=kappaSpace(comp);
    
    clear vF0
    OptContract;
    SB;

    if (kappa*q<=VB+0.025)

        beta0=betaB;
        betaT=betaB;

    else

        Solve;
        T=3;

    end
    Time;

    [m,ind1]=min(abs(solVTime.x-1));

    betaT=vbetat(end);
    beta1=vbetat(ind1);
    aT=vAt(end);

   
    
    
    vQ(comp)=q;
    vA0(comp)=a0;
    vABOut(comp)=aB;
    vAT(comp)=aT;
    vBeta0(comp)=beta0;
    vBetaB(comp)=betaB;
    vBetaT(comp)=betaT;
    vTau0(comp)=tau0;
    vF0stat(comp)=F0;
    vFrac(comp)=betaB/beta0;
    
    vFracBeta1(comp)=1-beta1/beta0;
    vFracBeta2(comp)=1-betaT/beta0;
    vFracBeta3(comp)=1-betaB/beta0;
    vDiffBeta(comp)=beta0-betaT;
    
    
  
    
    
end 

close all;
space=kappaSpace;


fFigure=figure(1); hold on;

subplot(2,4,1); hold on
 plot(space,smooth(vBeta0), 'k', 'Linewidth',3);
  plot(space,smooth(vBetaT), 'r:', 'Linewidth',3);
  plot(space,smooth(vBetaB), '-.', 'Linewidth',3);
  leg2=legend('$t=0$','$t=3$','$t=\infty$','Location','best'  ) ;
   set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$\kappa$','Interpreter','latex')
 ylabel('$\beta_{t}$','Interpreter','latex')
 title('A: Retention ($\kappa$)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;

 subplot(2,4,5); hold on
 plot(space,smooth(vFracBeta1,0.1), 'k-', 'Linewidth',3);
  plot(space,smooth(vFracBeta2,0.1), 'r:', 'Linewidth',3);
    plot(space,smooth(vFracBeta3), '-.', 'Linewidth',3);
 xlabel('$\kappa$','Interpreter','latex')
 ylabel('$1-\beta_{t}/\beta_0$','Interpreter','latex')
 leg2=legend('$t=0$','$t=3$','$t=\infty$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
  title('E: Selloff Speed ($\kappa$)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;
 


 
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%% PHI %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; 
para; 
%Note we choose a fine gride and low tolerance to obtain numerically
%accurate solutions. This increases the computation time
%Inaccuracy only arises for very high values of phi
qGrid=600;
options = odeset('AbsTol',1e-10,'RelTol',1e-10);
%For lower accuracy and computation time set, e.g., qGrid=200, options = odeset('AbsTol',1e-6,'RelTol',1e-6);


iComp=50;
%To speed up computation, choose lower iComp
phiSpace=linspace(6.5,150,iComp);


for comp=1:iComp
    
    phi=phiSpace(comp);
    clear vF0


    OptContract;
    SB;

    if (kappa*q<=VB+0.025)

        beta0=betaB;
        betaT=betaB;

    else

        Solve;
        T=3;

    end
    Time;

    [m,ind1]=min(abs(solVTime.x-2.5));

    betaT=vbetat(end);
    beta1=vbetat(ind1);
    aT=vAt(end);

   
    vBetaT(comp)=betaT;

    
    vQ(comp)=q;
    vA0(comp)=a0;
    vABOut(comp)=aB;
    vAT(comp)=aT;
    vBeta0(comp)=beta0;
    vBetaB(comp)=betaB;
    vTau0(comp)=tau0;
    vF0stat(comp)=F0;
    vFracBeta1(comp)=1-beta1/beta0;
    vFracBeta2(comp)=1-betaT/beta0;
    vFracBeta3(comp)=1-betaB/beta0;
    vDiffBeta(comp)=beta0-betaT;
    
  
    
    
end 

space=phiSpace;

fFigure=figure(1); hold on;

 subplot(2,4,2); hold on
 plot(space,smooth(vBeta0), 'k', 'Linewidth',3);
  plot(space,smooth(vBetaT), 'r:', 'Linewidth',3);
  plot(space,smooth(vBetaB), '-.', 'Linewidth',3);
  leg2=legend('$t=0$','$t=3$','$t=\infty$','Location','best'  ) ;
   set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$\phi$','Interpreter','latex')
 ylabel('$\beta_{t}$','Interpreter','latex')
 title('B: Retention ($\phi$)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;

 subplot(2,4,6); hold on
 plot(space,smooth(vFracBeta1), 'k-', 'Linewidth',3);
  plot(space,smooth(vFracBeta2), 'r:', 'Linewidth',3);
    plot(space,smooth(vFracBeta3), '-.', 'Linewidth',3);
 xlabel('$\phi$','Interpreter','latex')
 ylabel('$1-\beta_{t}/\beta_0$','Interpreter','latex')
 leg2=legend('$t=0$','$t=3$','$t=\infty$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
 title('F: Selloff Speed ($\phi$)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%% LAMBDA %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; 
para; 
qBar=0.25;
iComp=20;

LambdaSpace=linspace(0.925,1.1,iComp);


for comp=1:iComp
    
    Lambda=LambdaSpace(comp);
    clear vF0


    OptContract;
    SB;

    if (kappa*q<=VB+0.025)

        beta0=betaB;
        betaT=betaB;

    else

        Solve;
        T=3;

    end
    Time;

    [m,ind1]=min(abs(solVTime.x-2.5));

    betaT=vbetat(end);
    beta1=vbetat(ind1);
    aT=vAt(end);

   
    
    vBetaT(comp)=betaT;

    vQ(comp)=q;
    vA0(comp)=a0;
    vABOut(comp)=aB;
    vAT(comp)=aT;
    vBeta0(comp)=beta0;
    vBetaB(comp)=betaB;
    vTau0(comp)=tau0;
    vF0stat(comp)=F0;
    vFrac(comp)=betaB/beta0;
    vFracBeta1(comp)=1-beta1/beta0;
    vFracBeta2(comp)=1-betaT/beta0;
    vFracBeta3(comp)=1-betaB/beta0;
    vDiffBeta(comp)=beta0-betaT;
    
  
    
    
end 

space=LambdaSpace;

fFigure=figure(1); hold on;

 subplot(2,4,3); hold on
plot(space,smooth(vBeta0), 'k', 'Linewidth',3);
  plot(space,smooth(vBetaT), 'r:', 'Linewidth',3);
  plot(space,smooth(vBetaB), '-.', 'Linewidth',3);
  leg2=legend('$t=0$','$t=3$','$t=\infty$','Location','best'  ) ;
   set(leg2,'Interpreter','latex','FontSize',11);
 xlabel('$\Lambda$','Interpreter','latex')
 ylabel('$\beta_{t}$','Interpreter','latex')
  title('C: Retention ($\Lambda$)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;

 subplot(2,4,7); hold on
 plot(space,smooth(vFracBeta1), 'k-', 'Linewidth',3);
  plot(space,smooth(vFracBeta2), 'r:', 'Linewidth',3);
    plot(space,smooth(vFracBeta3), '-.', 'Linewidth',3);
 leg2=legend('$t=0$','$t=3$','$t=\infty$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',11);
 xlabel('$\Lambda$','Interpreter','latex')
  ylabel('$1-\beta_{t}/\beta_0$','Interpreter','latex')
  title('G: Selloff Speed ($\Lambda$)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%% GAMMA %%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all; 
para; 
iComp=20;

gammaSpace=linspace(0.04,0.18,iComp);


for comp=1:iComp
    
    gamma=gammaSpace(comp);
    clear vF0


    OptContract;
    SB;

    if (kappa*q<=VB+0.025)

        beta0=betaB;
        betaT=betaB;

    else

        Solve;
        T=3;

    end
    Time;

    [m,ind1]=min(abs(solVTime.x-2.5));

    betaT=vbetat(end);
    beta1=vbetat(ind1);
    aT=vAt(end);

   
    
    
    vQ(comp)=q;
    vA0(comp)=a0;
    vABOut(comp)=aB;
    vAT(comp)=aT;
    vBeta0(comp)=beta0;
    vBetaB(comp)=betaB;
    vTau0(comp)=tau0;
    vF0stat(comp)=F0;
    vFrac(comp)=betaB/beta0;
    vBetaT(comp)=betaT;

    vFracBeta1(comp)=1-beta1/beta0;
    vFracBeta2(comp)=1-betaT/beta0;
    vFracBeta3(comp)=1-betaB/beta0;
    vDiffBeta(comp)=beta0-betaT;
    
  
    
    
end 

space=gammaSpace;

fFigure=figure(1); hold on;

 subplot(2,4,4); hold on
 plot(space,smooth(vBeta0), 'k', 'Linewidth',3);
  plot(space,smooth(vBetaT), 'r:', 'Linewidth',3);
  plot(space,smooth(vBetaB), '-.', 'Linewidth',3);
  leg2=legend('$t=0$','$t=3$','$t=\infty$','Location','best'  ) ;
   set(leg2,'Interpreter','latex','FontSize',12);
 xlabel('$\gamma$','Interpreter','latex')
 ylabel('$\beta_{t}$','Interpreter','latex')
  title('D: Retention ($\gamma$)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;

 subplot(2,4,8); hold on
 plot(space,smooth(vFracBeta1), 'k-', 'Linewidth',3);
  plot(space,smooth(vFracBeta2), 'r:', 'Linewidth',3);
    plot(space,smooth(vFracBeta3), '-.', 'Linewidth',3);
 %plot(space,vQC, 'r:', 'Linewidth',3);
 xlabel('$\gamma$','Interpreter','latex')
  ylabel('$1-\beta_{t}/\beta_0$','Interpreter','latex')
 leg2=legend('$t=0$','$t=3$','$t=\infty$','Location','best'  ) ;
 set(leg2,'Interpreter','latex','FontSize',12);
  title('H: Selloff Speed ($\gamma$)','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;







resize_plot(fFigure,12,5.5,13);
print('CompStatRetentionGamma','-depsc')
%print('CompStatRetentionDelta','-depsc')





















